import time

# 未优化的代码
def unoptimized_io():
    start_time = time.time()
    for i in range(1000):
        with open('test.txt', 'a') as f:
            f.write(f"Line {i}\n")
    end_time = time.time()
    print(f"Unoptimized I/O took {end_time - start_time:.6f} seconds")

# 优化后的代码
def optimized_io():
    start_time = time.time()
    lines = [f"Line {i}\n" for i in range(1000)]  # 批量生成数据
    with open('test.txt', 'a') as f:
        f.writelines(lines)  # 批量写入文件
    end_time = time.time()
    print(f"Optimized I/O took {end_time - start_time:.6f} seconds")

if __name__ == "__main__":
    unoptimized_io()
    optimized_io()